package x.ovo.wechat.bot.contact;

import x.ovo.wechat.bot.common.contact.RetrievalStratrgy;
import x.ovo.wechat.bot.common.contact.RetrievalType;
import x.ovo.wechat.bot.common.entity.Contact;
import x.ovo.wechat.bot.common.http.WechatApi;

import java.util.Iterator;


/**
 * 根据用户名获取联系人策略
 *
 * @author ovo, created by 2024/04/22
 */
public class UserNameStrategy extends RetrievalStratrgy {

    private final WechatApi api;

    public UserNameStrategy(WechatApi api) {
        this.api = api;
    }

    @Override
    public Contact getContact(Iterator<Contact> iterator, String key) {
        while (iterator.hasNext()) {
            Contact account = iterator.next();
            if (account.getUserName().equals(key)) {
                return account;
            }
        }
        return this.api.getContactById(key);
    }

    @Override
    public void removeContact(String key) {

    }

    @Override
    public RetrievalType getType() {
        return RetrievalType.USER_NAME;
    }
}
